// pages/perCenter/withdraw/withdraw.js
// pages/perCenter/withdLast/withdLast.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
Page({
  data: {
    array: [],
    objectArray: [],
    index: 0,
    showModalStatus: false,
    banks:null,
    fee:0
  },
  getData: function () {
    var that = this;
    //加载银行卡信息
    util.request(api.BankList, { token: wx.getStorageSync("token") }).then(function (res) {
      if (res.code == '200') {
        var disArray = [];
        var bankList = res.datas;
        for(var i=0;i<bankList.length;i++){
          var displayStr = bankList[i].bankName +"(尾号"+ bankList[i].accountNumber.substring(bankList[i].accountNumber.length - 5, bankList[i].accountNumber.length)+"储蓄卡)";
          disArray.push(displayStr);
        }
        //体现信息默认为第一张银行卡
        var txInfo = {};
        txInfo["account"] = bankList[0].accountNumber;
        txInfo["accountName"] = bankList[0].accountName;
        txInfo['remark'] = '无';
        that.setData({
          banks: res.datas,
          array:disArray,
          txInfo:txInfo
        })
      }
    });
    //加载手续费
    util.request(api.GetFee, {total:that.data.divided}).then(function (res) {
      if(res.code=='200'){
        that.setData({
          fee:res.datas
        })
      }
    });
  },
  // start 弹出层
  powerDrawer: function (e) {
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu)
  },
  util: function (currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例   
    var animation = wx.createAnimation({
      duration: 200,  //动画时长  
      timingFunction: "linear", //线性  
      delay: 0  //0则不延迟  
    });
    // 第2步：这个动画实例赋给当前的动画实例  
    this.animation = animation;
    // 第3步：执行第一组动画  
    animation.opacity(0).rotateX(-100).step();
    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({
      animationData: animation.export()
    })
    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function () {
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({
        animationData: animation
      })
      //关闭  
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200)

    // 显示  
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
  }, // end 弹出层

  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({divided:options.divided})
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    this.getData();
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  bindPickerChange: function (e) {
    var that = this;
    var txInfo = {};
    var banks = that.data.banks;
    var index = parseInt(e.detail.value);
    txInfo["account"] = banks[index].accountNumber;
    txInfo["accountName"] = banks[index].accountName;
    txInfo['remark']='无';
    this.setData({
      index: index,
      txInfo:txInfo
    })
  },
  fnSureTx: function (e) {
    var that = this;
    var txInfo = that.data.txInfo;
    if (txInfo == null || txInfo.account == null || txInfo.account == "" || txInfo.accountName == null || txInfo.accountName == "" || that.data.divided == '' || that.data.divided == null || txInfo.remark == '' || txInfo.remark == null) {
      var msg = '请完善提现信息！'
      wx.showModal({
        title: '提示',
        content: msg,
      })
      return;
    }
    if (!util.checkBankNo(txInfo.account)) {
      msg = "银行账户无效!"
      wx.showModal({
        title: '提示',
        content: msg,
      })
      return;
    }
    var data = {
      price: that.data.divided,
      account: that.data.txInfo.account,
      accountName: that.data.txInfo.accountName,
      remark: that.data.txInfo.remark,
      token: wx.getStorageSync("token")
    }
    wx.showLoading({
      title: '加载中。。。',
      mask: true
    })
    util.request(api.Tx, data).then(function (res) {
      wx.hideLoading();
      if (res.code === '200') {
        wx.showToast({
          title: '申请成功',
          duration:1000
        });
        setTimeout(function(){
          wx.switchTab({
            url: '/pages/perCenter/myhomeN/myhomeN',
          })
        },1000);
      } else {
        wx.showToast({
          title: res.message,
        })
      }
    });
  }
})